<template>
    <div id="AsnDetailList">
        <v-tableTitle :btnConfig="btnConfig" :tableSelectData="selectData" :title="$t('outbound.lottablevalidation')" @backList="backList"
                      @del="del" @newCreate="newCreate" @save="save"></v-tableTitle>

        <div class="detail">
            <component :data="{detailData,argument}" :is="currentComponent"></component>
        </div>
    </div>

</template>

<script>
    import tableTitle from "commons/TableTitle";
    import navMenu from "commons/NavMenu";
    import baseMixin from "@/mixins/BaseMixin";

    export default {
        name: "AsnDetailList",
        mixins: [baseMixin],
        data() {
            return {
                newFlag: false,
                flag: false,
                argument: {},
                detailsFlag: false,
                detailData: {},
                selectData: [],
                detailNav: "1",
                componentConfig: [
                    {
                        name: "Table",
                        label: this.$t('config.tableDetail'),
                        component: () => import('./page/Table.vue')
                    },
                    {
                        name: "Detail",
                        label: this.$t('common.detail'),
                        component: () => import('./page/Detail.vue')
                    },
                ],
            }
        },
        computed: {
            btnConfig() {
                if (this.newFlag) {
                    return []
                } else if (this.detailNav == 1) {
                    return [
                        {label: this.$t('common.create'), certigier: this.$parent.newStatus||(!this.btnPermNameList.includes('EN02040105')&&!this.btnPermNameList.includes('WM03090205')), invalid: "newCreate"},
                        {label: this.$t('common.delete'), certigier: this.$parent.deleteStatus||(!this.btnPermNameList.includes('EN02040106')&&!this.btnPermNameList.includes('WM03090206')), disabled: true, invalid: "del"},
                    ]
                } else if (this.detailNav != 1) {
                    return [
                        {label: this.$t('common.save'), certigier: this.$parent.saveStatus||(!this.btnPermNameList.includes('EN02040107')&&!this.btnPermNameList.includes('WM03090207')), invalid: "save"},
                        {label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('EN02040108')&&!this.btnPermNameList.includes('WM03090208'), invalid: "backList"},
                    ]
                }
            },
            currentComponent() {
                return this.componentConfig[this.detailNav - 1].component;
            }
        },
        methods: {
            //保存
            async save() {
                if (this.flag) {
                    this.detailData.lottablevalidationkey = this.argument.lottablevalidationkey
                    let data = {
                        lottablevalidationkey: this.argument.lottablevalidationkey,
                        lottablevalidationdetail: this.detailData
                    }
                    try {
                        let res = await this.$api.post(`/insertLottable/${this.$store.state.wms}`, data);
                        
                            this.$message.success(res.returnMgr);
                            this.detailNav = '1';
                        
                    } catch (e) {
                        this.$message.error(e);
                    }
                } else {
                    try {
                        let res = await this.$api.post(`/updateLottabledetail/${this.$store.state.wms}`, this.detailData);
                        
                            this.$message.success(res.returnMgr);
                            this.detailNav = '1';
                        
                    } catch (e) {
                        this.$message.error(e);
                    }
                }
            },
            //删除数据
            async del(data) {
                try {
                    let res = await this.$api.post(`/deleteLottabledetail/${this.$store.state.wms}`, data);
                    
                        this.$message.success(res.returnMgr);
                        this.$children[1].getTableData(1);
                        this.$children[1].$refs.table.delSelectData(data);
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },

            //返回试图
            backList() {
                this.detailNav = '1';
            },
            //新建
            async newCreate() {
                try {
                    let res = await this.$api.get(`/queryLottabledetailMaxLine/${this.argument.lottablevalidationkey}/${this.$store.state.wms}`)
                    
                        this.detailNav = '2';
                        this.detailData = {
                            seq: res.data.seq,
                            lottable01receiptvalidation: 0,
                            lottable02receiptvalidation: 0,
                            lottable03receiptvalidation: 0,
                            lottable04receiptvalidation: 0,
                            lottable05receiptvalidation: 0,
                            lottable06receiptvalidation: 0,
                            lottable07receiptvalidation: 0,
                            lottable08receiptvalidation: 0,
                            lottable09receiptvalidation: 0,
                            lottable10receiptvalidation: 0,
                            lottable11receiptvalidation: 0,
                            lottable12receiptvalidation: 0,
                            lottable01receiptconversion: "NONE",
                            lottable02receiptconversion: "NONE",
                            lottable03receiptconversion: "NONE",
                            lottable04receiptconversion: " ",
                            lottable05receiptconversion: " ",
                            lottable06receiptconversion: "NONE",
                            lottable07receiptconversion: "NONE",
                            lottable08receiptconversion: "NONE",
                            lottable09receiptconversion: "NONE",
                            lottable10receiptconversion: "NONE",
                            lottable11receiptconversion: "NONE",
                            lottable12receiptconversion: "NONE",
                        }
                        this.flag = true;
                    
                } catch (e) {
                    this.$message.error(e);
                }

            },

            async enterDetails(data) {
                try {
                    let res = await this.$api.get(`/queryLottabledetailByKey/${this.argument.lottablevalidationkey}/${data.seq}/${this.$store.state.wms}`);
                    
                        this.detailData = res.bean;
                        this.detailsFlag = true;
                        this.detailNav = '2';
                    
                } catch (e) {
                    this.$message.error(e);
                }
            }

        },
        mounted() {
            this.newFlag = this.$store.state.newFlag;
            if (Object.keys(this.$store.state.skip).length !== 0) {
                let data = this.$store.state.skip;
                this.argument = data.routeData;
                this.detailData = data.detailList;
                this.detailNav = data.detailListNav;
            } else if (this.newFlag) {
                this.detailData = {
                    seq: "00001",
                    lottable01receiptvalidation: "0",
                    lottable02receiptvalidation: "0",
                    lottable03receiptvalidation: "0",
                    lottable04receiptvalidation: "0",
                    lottable05receiptvalidation: "0",
                    lottable06receiptvalidation: "0",
                    lottable07receiptvalidation: "0",
                    lottable08receiptvalidation: "0",
                    lottable09receiptvalidation: "0",
                    lottable10receiptvalidation: "0",
                    lottable11receiptvalidation: "0",
                    lottable12receiptvalidation: "0",
                    lottable01receiptconversion: "NONE",
                    lottable02receiptconversion: "NONE",
                    lottable03receiptconversion: "NONE",
                    lottable04receiptconversion: " ",
                    lottable05receiptconversion: " ",
                    lottable06receiptconversion: "NONE",
                    lottable07receiptconversion: "NONE",
                    lottable08receiptconversion: "NONE",
                    lottable09receiptconversion: "NONE",
                    lottable10receiptconversion: "NONE",
                    lottable11receiptconversion: "NONE",
                    lottable12receiptconversion: "NONE",
                };
                this.detailNav = "2";
            } else {
                this.argument = this.$store.state.routeData;
            }
        },
        components: {
            "v-tableTitle": tableTitle,
            "v-navMenu": navMenu,
        },
        watch: {
            selectData(data) {
                if (this.$parent.permissionStatus) {
                    if (data.length > 0) {
                        this.$parent.deleteStatus = false;
                    } else {
                        this.$parent.deleteStatus = true;
                    }
                }
            },
            detailNav(data) {
                if (data == "1") {
                    this.flag = false;
                    this.detailsFlag = false;
                } else {
                    this.detailsFlag = true;
                }
            }
        }
    }
</script>

<style>

</style>
